%--------------------------------------------------------------------------
%This program extracts the largest strongly connected set of establishments
%from the BeH. Note only establishments that hire from N are considered.
%--------------------------------------------------------------------------

clc;
clear all;

cd '\\employers\matlab\sconn_idfy';
addpath('\\employers\matlab\functions');


%-------------------------------------------------------------------------%
% create looping variables for sample windows
%-------------------------------------------------------------------------%

%empty vector to store versions 
vervec = ones(35,1);

%declare first set of versions (these cover IAB pre-set windows and pooled sample)
vervec(1) = 3; 

%declare custom windows 
c = 2; 
for w = 3:4:7
    for y = 1997:1:2019
        if y + (w-1)/2 <2019 && y - (w-1)/2 >1997   
            ver = y*10 + w;
            vervec(c) = ver;
            c = c+1;
        end
    end
end
clear c;

%-------------------------------------------------------------------------%
%loop over sample windows
%-------------------------------------------------------------------------%

allv = 1;


for i = allv:length(vervec) %loop starter is either allv or cusv
    ver = vervec(i)
    
    %--------------------------------------------------------------------------
    % All moves dataset
    %--------------------------------------------------------------------------

    fnamein = ['flows_ver',num2str(ver),'.csv'];
    fnameout = ['sconn_idfy_ver',num2str(ver),'.csv'];
    clear ver

    all_moves = csvread(fnamein,1,0);
    %drop the row of flows
    all_moves = all_moves(:,1:2);

    %find the strongly connected set
    D = [all_moves ones(length(all_moves),1)];
    A = spconvert(D); %adjacency matrix
    clear D;
    [sci,sizes,rp,ci]=scomponents(A);
    clear A;
    [csize,cind] = max(sizes);
    sconn = sci==cind; %identify estabs in the largest strongly connected set
    %output the id's and indicators of belonging to the strongly connected set
    %to csv
    sconn = [(1:1:length(sconn))' sconn];
    dlmwrite(fnameout,sconn,'precision','%10.9g');
    
end